package com.xspace.android.xafcommon.util.commonUtil;

import android.text.TextUtils;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.util.LogUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * 获取模板的解析类
 * @author Administrator
 *
 */
public class PullParserUtils {
	//public static Map<String,ModelBean> ModelList;
	//	public static String getModelClass(String key) {
	//		if(ModelList==null){
	//			ParseModelContent();
	//		}
	//		return ModelList.get(key).value;
	//	}
	public static List<ModuleBean> ParseModelContent(String xmlName) {
		if (TextUtils.isEmpty(xmlName))
			return null;
		InputStream inputStream = null;
		List<ModuleBean> modelList = null;
		LogUtil.i("model", "ParseModelContent xmlName:" + xmlName);
		try {
			inputStream = BaseApplication.mInstance.getAssets().open(xmlName + ".xml");
			modelList = parserXmlByPull(inputStream);
			//			Iterator<Entry<String, ModelBean>> iteror = modelList.entrySet().iterator();
			//			while (iteror.hasNext()) {
			//				Map.Entry<String, ModelBean> entry = (Map.Entry<String, ModelBean>) iteror.next();
			//				LogUtil.v("model", "Model bean:" + entry.getValue().beanToString());
			//			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return modelList;
	}

	public static List<ModuleBean> parserXmlByPull(InputStream inputStream) throws Exception {
		List<ModuleBean> persons = null;
		List<ModuleBean.ToClass> classList = null;
		List<ModuleBean.ToClass.Extra> extraList = null;
		ModuleBean person = null;
		ModuleBean.ToClass toClass = null;
		ModuleBean.ToClass.Extra extra = null;
		//    创建XmlPullParserFactory解析工厂
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		//    通过XmlPullParserFactory工厂类实例化一个XmlPullParser解析类
		XmlPullParser parser = factory.newPullParser();
		//    根据指定的编码来解析xml文档
		parser.setInput(inputStream, "utf-8");

		//    得到当前的事件类型
		int eventType = parser.getEventType();
		String pkey = null;
		//    只要没有解析到xml的文档结束，就一直解析
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			//    解析到文档开始的时候
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList<ModuleBean>();
				// XmlPullParser.
				break;
			//    解析到xml标签的时候
			case XmlPullParser.START_TAG:
				if ("ModuleList".equals(parser.getName())) {
					//    得到person元素的第一个属性，也就是pKey
					pkey = parser.getAttributeValue(0);
				} else if ("Module".equals(parser.getName())) {
					person = new ModuleBean();
					//    如果是name元素，则通过nextText()方法得到元素的值
					// person.setName(parser.nextText());
				} else if ("ModelName".equals(parser.getName())) {
					person.setModelName(parser.nextText());
				} else if ("PackageName".equals(parser.getName())) {
					person.setPackageName(parser.nextText());
				} else if ("ApkName".equals(parser.getName())) {
					person.setApkName(parser.nextText());
				} else if ("Version".equals(parser.getName())) {
					person.setVersion(parser.nextText());
				} else if ("Class".equals(parser.getName())) {
					toClass = person.new ToClass();
				} else if ("ClassList".equals(parser.getName())) {
					classList = new ArrayList<ModuleBean.ToClass>();
				} else if ("ExtraList".equals(parser.getName())) {
					extraList = new ArrayList<ModuleBean.ToClass.Extra>();
				} else if ("ClassNameToAct".equals(parser.getName())) {
					toClass.setClassName(parser.nextText());
				} else if ("Key".equals(parser.getName())) {
					toClass.setKey(parser.nextText());
				} else if ("Extra".equals(parser.getName())) {
					extra = toClass.new Extra();
				} else if ("DataKey".equals(parser.getName())) {
					extra.setKey(parser.nextText());
				} else if ("Value".equals(parser.getName())) {
					extra.setValue(parser.nextText());
				} else if ("ClassType".equals(parser.getName())) {
					toClass.setClassType(parser.nextText());
				}
				break;
			//    解析到xml标签结束的时候
			case XmlPullParser.END_TAG:
				if ("Module".equals(parser.getName())) {
					persons.add(person);
					person = null;
				} else if ("Class".equals(parser.getName())) {
					classList.add(toClass);
					toClass = null;
				} else if ("ClassList".equals(parser.getName())) {
					person.setClassList(classList);
					classList = null;
				} else if ("ExtraList".equals(parser.getName())) {
					toClass.setExtraList(extraList);
					extraList = null;
				} else if ("Extra".equals(parser.getName())) {
					extraList.add(extra);
					extra = null;
				}
				break;
			}
			//    通过next()方法触发下一个事件
			eventType = parser.next();
		}
		return persons;
	}
}